﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Mono.Cecil;
using ProjScan.Modules;
using ProjScan.RecordManager.AssetData;
using ProjScan.Submodules.CS_Check;
using ProjScan.Tools;

namespace ProjScan.RecordManager
{
	// Token: 0x0200028E RID: 654
	internal class TypeDefinitionRecordManager : RecordManagerBase<TypeDefinitionAssetData>
	{
		// Token: 0x1700038A RID: 906
		// (get) Token: 0x06001139 RID: 4409 RVA: 0x0000854B File Offset: 0x0000674B
		public override string TableTag
		{
			get
			{
				return "script";
			}
		}

		// Token: 0x0600113A RID: 4410 RVA: 0x00051084 File Offset: 0x0004F284
		public override void PrepareAssets()
		{
			this.datas = new List<TypeDefinitionAssetData>();
			List<AssemblyDetails> assemblyDetails = Singleton<AssemblyManager>.Instance.GetAssemblyDetails();
			DefaultAssemblyResolver defaultAssemblyResolver = new DefaultAssemblyResolver();
			List<string> list = new List<string>();
			list.AddRange(ApiCompatibilityUtils.Instance.GetEnvironmentVariables());
			foreach (string text in list)
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, Module.CS_Check, ScanLog.Stage.s2, "ScriptRecordManager:Scan:SearchDirectory 1Pass", text);
				defaultAssemblyResolver.AddSearchDirectory(text);
			}
			foreach (string text2 in Enumerable.Distinct<string>(Enumerable.Select<AssemblyDetails, string>(assemblyDetails, (AssemblyDetails info) => Path.GetDirectoryName(info.filePath))))
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, Module.CS_Check, ScanLog.Stage.s2, "ScriptRecordManager:Scan:SearchDirectory 2Pass", text2);
				defaultAssemblyResolver.AddSearchDirectory(text2);
			}
			List<AssemblyDetails> list2 = new List<AssemblyDetails>();
			foreach (AssemblyDetails assemblyDetails2 in assemblyDetails)
			{
				bool flag = assemblyDetails2.asmType != AssemblyDetails.AsmType.InPackages;
				if (flag)
				{
					list2.Add(assemblyDetails2);
				}
			}
			for (int i = 0; i < list2.Count; i++)
			{
				AssemblyDefinition assemblyDefinition;
				try
				{
					assemblyDefinition = AssemblyDefinition.ReadAssembly(list2[i].filePath, new ReaderParameters
					{
						ReadSymbols = true,
						AssemblyResolver = defaultAssemblyResolver
					});
				}
				catch (Exception ex)
				{
					ScanLog instance = ScanLog.Instance;
					ScanLog.LogLevel logLevel = ScanLog.LogLevel.EXCEPTION;
					Module module = Module.CS_Check;
					string tableTag = this.TableTag;
					string[] array = new string[5];
					int num = 0;
					Type type = ex.GetType();
					array[num] = ((type != null) ? type.ToString() : null);
					array[1] = "\n";
					array[2] = ex.Message;
					array[3] = "\n";
					array[4] = ex.StackTrace;
					instance.LogInfo(logLevel, module, tableTag, string.Concat(array));
					ScanLog.UnityLogError("CS Check: Cannot read symbol");
					try
					{
						assemblyDefinition = AssemblyDefinition.ReadAssembly(list2[i].filePath, new ReaderParameters());
					}
					catch (Exception ex2)
					{
						ScanLog instance2 = ScanLog.Instance;
						ScanLog.LogLevel logLevel2 = ScanLog.LogLevel.EXCEPTION;
						Module module2 = Module.CS_Check;
						string tableTag2 = this.TableTag;
						string[] array2 = new string[5];
						int num2 = 0;
						Type type2 = ex2.GetType();
						array2[num2] = ((type2 != null) ? type2.ToString() : null);
						array2[1] = "\n";
						array2[2] = ex2.Message;
						array2[3] = "\n";
						array2[4] = ex2.StackTrace;
						instance2.LogInfo(logLevel2, module2, tableTag2, string.Concat(array2));
						break;
					}
				}
				foreach (TypeDefinition typeDefinition in assemblyDefinition.MainModule.Types)
				{
					bool hasMethods = typeDefinition.HasMethods;
					if (hasMethods)
					{
						TypeDefinitionAssetData typeDefinitionAssetData = null;
						try
						{
							typeDefinitionAssetData = new TypeDefinitionAssetData(typeDefinition.FullName, typeDefinition);
						}
						catch (Exception ex3)
						{
							ScanLog instance3 = ScanLog.Instance;
							ScanLog.LogLevel logLevel3 = ScanLog.LogLevel.EXCEPTION;
							Module module3 = Module.CS_Check;
							string tableTag3 = this.TableTag;
							string[] array3 = new string[5];
							int num3 = 0;
							Type type3 = ex3.GetType();
							array3[num3] = ((type3 != null) ? type3.ToString() : null);
							array3[1] = "\n";
							array3[2] = ex3.Message;
							array3[3] = "\n";
							array3[4] = ex3.StackTrace;
							instance3.LogInfo(logLevel3, module3, tableTag3, string.Concat(array3));
						}
						string text3 = ToolsHelper.ConvertAbsolutePathToRelative(ToolsHelper.DirConvert(typeDefinitionAssetData.path));
						this.ActiveAllRecord();
						bool flag2 = string.IsNullOrEmpty(text3) || this.CheckAssetPathRules(text3);
						if (flag2)
						{
							typeDefinitionAssetData.id = this.datas.Count;
							this.ValidateAsset(typeDefinitionAssetData);
							typeDefinitionAssetData.Reset();
							this.datas.Add(typeDefinitionAssetData);
						}
					}
				}
			}
		}
	}
}
